import type { AxiosInstance } from 'axios'

import type { ApiResult } from './types'

function getAction(instance: AxiosInstance) {
  return <T = undefined>(url: string, params?: unknown) => {
    return instance.get<ApiResult<T>>(url, {
      params
    })
  }
}

function postAction(instance: AxiosInstance) {
  return <T = undefined>(url: string, params?: unknown) => {
    return instance.post<ApiResult<T>>(url, {
      data: params
    })
  }
}

function deleteAction(instance: AxiosInstance) {
  return <T = undefined>(url: string, params?: unknown) => {
    return instance.delete<ApiResult<T>>(url, {
      data: params
    })
  }
}

export function createAction(axiosInstance: AxiosInstance) {
  return {
    getAction: getAction(axiosInstance),
    postAction: postAction(axiosInstance),
    deleteAction: deleteAction(axiosInstance)
  }
}
